﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucUnit
    Dim db As New TanNamChinhDataContext
    Private m_unit As New CUnit

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgUnit)
    End Sub

    Private Sub ucUnit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        KhoiTao_GiaoDien(True)
        KhoiTaodtgUnit()
    End Sub

    Public Sub KhoiTaodtgUnit(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Dim fc = m_unit.SelectAllRecordByName(Name)

        Dim source As New BindingSource
        source.DataSource = fc
        BindingNavigator1.BindingSource = source

        dtgUnit.DataSource = source
        FormatdtgUnit()

        Try
            If NameUpdate = "" Then
                dtgUnit.Rows(0).Selected = True
                dtgUnit.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgUnit.RowCount - 1
                    If dtgUnit.Rows(i).Cells("UnitName").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgUnit.Rows(index).Cells("UnitName").Selected = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub FormatdtgUnit()
        'dtgUnit.RowHeadersWidth = 20
        dtgUnit.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgUnit.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dtgUnit.MultiSelect = False
        dtgUnit.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgUnit.ReadOnly = True
        dtgUnit.BackgroundColor = Color.White
        dtgUnit.AllowUserToAddRows = False
        dtgUnit.AllowUserToDeleteRows = False

        dtgUnit.Columns("ID_Unit").Visible = False
        dtgUnit.Columns("IsActive").Visible = False
        With dtgUnit.Columns("UnitName")
            .HeaderText = "Unit"
        End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtUnit.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtUnit.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Unit As Integer = dtgUnit.CurrentRow.Cells("ID_Unit").Value
        'kiem tra truoc khi xoa
        Dim FCName As String = dtgUnit.CurrentRow.Cells("UnitName").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_unit.Delete(ID_Unit)
                KhoiTaodtgUnit()
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Unit Name {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Unit: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_ac As New tblUnit
        obj_ac.UnitName = txtUnit.Text

        If f_Them Then
            obj_ac.IsActive = True
            m_unit.Insert(obj_ac)
            KhoiTaodtgUnit(txtUnit.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Unit Name {0}", obj_ac.UnitName))
        End If

        If f_Sua Then
            obj_ac.ID_Unit = dtgUnit.CurrentRow.Cells("ID_Unit").Value
            m_unit.Update(obj_ac)
            KhoiTaodtgUnit(txtUnit.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Unit Name {0}", obj_ac.UnitName))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgUnit(dtgUnit.CurrentRow.Cells("UnitName").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtUnit.ReadOnly = flag

        dtgUnit.Enabled = flag

        'If flag Then
        '    txtUnit.BackColor = Color.Azure
        'Else
        '    txtUnit.BackColor = Color.White
        'End If
    End Sub

    Public Sub Clear()
        txtUnit.Text = ""
    End Sub
#End Region

    Private Sub dtgUnit_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgUnit.CurrentCellChanged
        Try
            txtUnit.Text = dtgUnit.CurrentRow.Cells("UnitName").Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgUnit("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgUnit("", txtNameSearch.Text)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            KhoiTaodtgUnit("", txtNameSearch.Text.Trim)
        End If
    End Sub
End Class
